package com.demo.producer;

import com.demo.common.Constant;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.nio.charset.StandardCharsets;

/**
 * @author yexi
 */
public class ProducerProperty {
    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        DefaultMQProducer defaultMQProducer = new DefaultMQProducer(Constant.PRODUCER_GROUP_1);
        defaultMQProducer.setNamesrvAddr(Constant.ROCKET_MQ_ADDRESS);
        defaultMQProducer.start();
        String topic = "05-filter";
        String tags = "tagA";
        String msg = "RocketMQ Property Send ";
        Message message = new Message(
                topic,
                tags,
                (msg).getBytes(StandardCharsets.UTF_8)
        );
        message.putUserProperty("age","16");
        SendResult sendResult = defaultMQProducer.send(message);
        System.out.println("成功发送的消息ID: " + sendResult.getMsgId());
        defaultMQProducer.shutdown();
    }
}
